1   package org.apache.lucene.analysis.ja.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import java.io.BufferedOutputStream;
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStream;
25  
26  import org.apache.lucene.analysis.ja.dict.ConnectionCosts;
27  
28  import org.apache.lucene.codecs.CodecUtil;
29  import org.apache.lucene.store.DataOutput;
30  import org.apache.lucene.store.OutputStreamDataOutput;
31  import org.apache.lucene.util.BitUtil;
32  
33  public final class ConnectionCostsWriter {
34    
35    private final short[][] costs; // array is backward IDs first since get is called using the same backward ID consecutively. maybe doesn't matter.
36    private final int forwardSize;
37    private final int backwardSize;
38    /**
39     * Constructor for building. TODO: remove write access
40     */
41    public ConnectionCostsWriter(int forwardSize, int backwardSize) {
42      this.forwardSize = forwardSize;
43      this.backwardSize = backwardSize;
44      this.costs = new short[backwardSize][forwardSize];
45    }
46    
47    public void add(int forwardId, int backwardId, int cost) {
48      this.costs[backwardId][forwardId] = (short)cost;
49    }
50    
51    public void write(String baseDir) throws IOException {
52      String filename = baseDir + File.separator +
53        ConnectionCosts.class.getName().replace('.', File.separatorChar) + ConnectionCosts.FILENAME_SUFFIX;
54      new File(filename).getParentFile().mkdirs();
55      OutputStream os = new FileOutputStream(filename);
56      try {
57        os = new BufferedOutputStream(os);
58        final DataOutput out = new OutputStreamDataOutput(os);
59        CodecUtil.writeHeader(out, ConnectionCosts.HEADER, ConnectionCosts.VERSION);
60        out.writeVInt(forwardSize);
61        out.writeVInt(backwardSize);
62        int last = 0;
63        assert costs.length == backwardSize;
64        for (short[] a : costs) {
65          assert a.length == forwardSize;
66          for (int i = 0; i < a.length; i++) {
67            int delta = (int)a[i] - last;
68            out.writeZInt(delta);
69            last = a[i];
70          }
71        }
72      } finally {
73        os.close();
74      }
75    }
76    
77  }